$(function(){
	
});

function exit(){
	$("#meId").val("");
	$("#meNames").val("");
	$("#mePwd").val("");
	$("#mePhone").val("");
	$("#meEmail").val("");
	$("input[type='radio']").removeAttr('checked');
	load();
	//关闭所有页面层
	layer.closeAll('page');
}
layui.use(['form', 'layedit', 'laydate','table','upload'], function(){
	var table = layui.table
	,laydate = layui.laydate
	,form = layui.form
	,layer = layui.layer
	,layedit = layui.layedit
	,upload = layui.upload;

	//普通图片上传
	var uploadInst = upload.render({
		elem: '#meImage'
			,url: '/upload/'
				,auto:0
				,choose: function(obj){
					//预读本地文件示例，不支持ie8
					obj.preview(function(index, file, result){
						$('#img').attr('src', result); //图片链接（base64）
						$("#img").css({"width":"35px","height":"35px"});
					});
				}
	});

	//监听工具条
	table.on('tool(demo)', function(obj){
		var da = obj.data;

		if(obj.event === 'edit'){
			var url="../../member/findById.action";
			var data={"meId":da.meId};
			$.post(url,data,function(mes){
				layer.open({
					type: 1	 //此处以iframe举例
					,title: '数据修改'
						,area: ['600px', '530px']
				,offset: 'auto'
					,content:$('#addOrUpdate')
					,success: function(layero){
						$("#meId").val(mes.meId);
						$("#meNames").val(mes.meName);
						$("#mePwd").val(mes.mePwd);
						$("#mePhone").val(mes.mePhone);
						$("#meEmail").val(mes.meEmail);
						$("input[name='meSex']").each(function(){ 
	                		if ($(this).val() == mes.meSex) {
	                			$(this).prop("checked",true);
	                		} 
	            		});
						$("input[name='meDiscount']").each(function(){ 
	                		if ($(this).val() == mes.meDiscount) {
	                			$(this).prop("checked",true);
	                		} 
	            		});
						$("#img").attr({"src":"../../"+mes.meImage});
						//渲染
						load();
					},end : function() {
						//弹框关闭的回调函数
						$("#addOrUpdate").hide();
						exit();
					}
				});

			},"json");
		}else if(obj.event === 'del'){
			layer.confirm('真的删除行么', function(index){
				var url="../../member/delete.action";
				var data={"meId":da.meId}
				$.post(url,data,function(mes){
					if(mes==1){
						obj.del();
						layer.close(index);
						layer.msg("删除成功");
					}else{
						layer.msg("删除失败");
					}
				},"json");

			});
		}

	});
	var $ = layui.$, active = {
			reload: function(){
				var meName = $('#meName');
				//执行重载
				table.reload('idTest', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
				,where: {
					meName: meName.val()
				}
				});
			}
	};

	$('.demoTable .layui-btn').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});

	$('.demoTable #insert').on('click', function(){
		layer.open({
			type: 1	 //此处以iframe举例
			,title: '增加会员'
				,area: ['600px', '530px']
		,offset: 'auto'
			,content:$('#addOrUpdate')
			,end : function() {
				$("#operUser").attr({"disabled":false});
				//弹框关闭的回调函数
				$("#addOrUpdate").hide();
				exit();
			}
		});
	});

});

function findCiSales(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#meNames").val()==null || $("#meNames").val()==''){
			 parent.layer.msg('会员名称不能为空', {
			      icon: 5,
			      anim :6
			  });
			 $("#meNameYz").val("×");
		}else if($("#meNames").val().length>10){
			 parent.layer.msg('会员名称不能超过10长度', {
			      icon: 5,
			      anim :6
			  });
			 $("#meNameYz").val("×");
		}else{
			 $("#meNameYz").val("√");
		}
	});
}
function findmePwd(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#mePwd").val()==null || $("#mePwd").val()==''){
			 parent.layer.msg('会员密码不能为空', {
			      icon: 5,
			      anim :6
			  });
		}else if($("#mePwd").val().length>10){
			 parent.layer.msg('会员密码不能超过10长度', {
			      icon: 5,
			      anim :6
			  });
		}
	});
}
function findmePhone(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#mePhone").val()==null || $("#mePhone").val()==''){
			 parent.layer.msg('会员电话不能为空', {
			      icon: 5,
			      anim :6
			  });
		}else if(isNaN($("#mePhone").val())){
			 parent.layer.msg('会员电话只能是数字', {
			      icon: 5,
			      anim :6
			  });
		}
	});
}

function FromSubmit(){
	findmePwd();
	findmePhone();
	findCiSales();
	
	var countyz=0;
	$.each($(".fromYz"),function(index,item){
		if($(item).val()=="√"){
			countyz++;
		}
	});
	if(countyz==1){
		$("#myform").submit();
	}
}
function load() {
	var form = layui.form
	form.render();
}